/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef OTASESSION_H
#define OTASESSION_H

/**
	@author Warren Kenny <warren.kenny@gmail.com>
*/

#include <vector>

#include <otaCore.h>
#include <otaSessionKey.h>
#include <otaClientCommand.h>
#include <otaClientCredentials.h>
#include <otaServerHandshake.h>
#include <otaServerResponse.h>
#include <otaSessionListener.h>

enum otaSessionState
{
	OTA_SESSION_INVALID,
	OTA_SESSION_UNINITIALIZED,
 	OTA_SESSION_CLIENT_CREDENTIALS_PENDING,
  	OTA_SESSION_CLIENT_LOGON_CONFIRMATION_PENDING,
   	OTA_SESSION_CLIENT_CREDENTIALS_OLD_FORMAT_PENDING,
   	OTA_SESSION_CLIENT_LOGON_FAILURE,
  	OTA_SESSION_INIT_DB_CONFIRMATION_PENDING,
   	OTA_SESSION_ACCEPTING_COMMANDS,
    	OTA_SESSION_READY,
};


class otaSession
{
public:
	otaSession();
	
	void registerListener( otaSessionListener* listener );
	
	void setInitData( const otaSessionKey key, unsigned long serverIP, unsigned short serverPort, unsigned long clientIP, unsigned short clientPort );
	
	void addPacket( const otaSessionKey& key, const otaTime arrivalTime, const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset );
	
	bool getSessionID( unsigned long &sessionID );
	
	void setSessionID( unsigned long sessionID );
	
	unsigned long getServerIP();
	unsigned long getClientIP();
	
	unsigned short getServerPort();
	unsigned short getClientPort();
	
	unsigned int getThreadID();
	
	std::string getUsername();
	
	otaTime getStartTime();
	otaTime getEndTime();
	
	bool getCommand( unsigned long &sequenceID,  otaClientCommand** command);
	
	bool isValid();
	
	~otaSession();

protected:
	
	bool sourceServer( const otaSessionKey& key );
	
private:	
	
	unsigned int 	m_state;
	
	otaSessionKey 	m_myKey;
	
	// Network Data
	
	unsigned long 	m_serverIP;
	unsigned long 	m_clientIP;
	
	unsigned short 	m_serverPort;
	unsigned short 	m_clientPort;
	
	// Session Data
	
	bool		m_idSet;
	unsigned long 	m_sessionID;
	unsigned long	m_sequenceID;
	unsigned long	m_lastSequence;
	
	unsigned int 	m_serverThreadID;
	
	unsigned int	m_clientFlags;
	std::string	m_username;
	
	std::string	m_database;
	std::string	m_tempDatabase;
	
	otaTime		m_sessionStart;
	otaTime		m_sessionLastPacket;
	
	std::map<unsigned long, otaClientCommand> m_clientCommands;
	
	// Listeners for session events
	
	std::vector<otaSessionListener *> m_listeners;
	
};

#endif
